package 线程.demo10;

import java.sql.Array;
import java.util.Arrays;

/**
 * @author wql
 * @version 1.0
 * @date 2022/11/1 15:27
 */

public class WqlSafe {
    private static  String[] arr= new String[2];
    private static  int index=0;
    private  static  Object lock= new Object();

    public static void main(String[] args) throws Exception{
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    if (arr[index]==null){
                        arr[index]="Hello";
                        index++;
                    }
                }
            }
        });
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    if (arr[index]==null){
                        arr[index]="World";
                        index++;
                    }
                }
            }
        });
        thread.start();
        thread1.start();
        thread.join();
        thread1.join();
        System.out.println(Arrays.toString(arr));
    }
}
